//////////////////////////////////////////////////////////////////////////////////////////////////////////
//		            BulletUnfreezeHinge
//////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma warning (disable: 4819)
#include "CKAll.h"
#include "BulletManager.h"

#define INPUT_PARAMENTER_CONSTRAINTID				0
#define INPUT_PARAMENTER_LOWERLIMIT					1
#define INPUT_PARAMENTER_UPPERLIMIT					2
#define INPUT_PARAMENTER_SOFTNESS						3
#define INPUT_PARAMENTER_BIASFACTOR					4

CKObjectDeclaration	*FillBehaviorBulletUnfreezeHingeDecl();
CKERROR CreateBulletUnfreezeHingeProto(CKBehaviorPrototype **);
int BulletUnfreezeHinge(const CKBehaviorContext& BehContext);

CKObjectDeclaration	*FillBehaviorBulletUnfreezeHingeDecl()
{
	CKObjectDeclaration *od = CreateCKObjectDeclaration("BulletUnfreezeHinge");	
	
	od->SetType(CKDLL_BEHAVIORPROTOTYPE);
	od->SetVersion(0x00000001);
	od->SetCreationFunction(CreateBulletUnfreezeHingeProto);
	od->SetDescription("Bullet Modify Hinge ");
	od->SetCategory("Bullet/Constraint");
	od->SetGuid(CKGUID(0x13367e6c,0x72a66d4a));
	od->SetAuthorGuid(CKGUID(0x457d2f46,0x3ee809a8));
	od->SetAuthorName("gjfeilipu");
	od->SetCompatibleClassId(CKCID_BEOBJECT);

	return od;
}

CKERROR CreateBulletUnfreezeHingeProto(CKBehaviorPrototype** pproto)
{
	CKBehaviorPrototype *proto = CreateCKBehaviorPrototype("BulletUnfreezeHinge");
	if (!proto) {
		return CKERR_OUTOFMEMORY;
	}

	proto->DeclareInput("In");
	proto->DeclareOutput("Out");

	proto->DeclareInParameter("Constraint ID",CKPGUID_INT,"-1");
	proto->DeclareInParameter("LowerLimit",CKPGUID_FLOAT,"0");
	proto->DeclareInParameter("UpperLimit",CKPGUID_FLOAT,"0");
	proto->DeclareInParameter("Softness",CKPGUID_FLOAT,"0.9");
	proto->DeclareInParameter("BiasFactor",CKPGUID_FLOAT,"0.3");

	//proto->DeclareOutParameter("Constraint ID",CKPGUID_INT,"-1");
	//proto->DeclareOutParameter("Angle",CKPGUID_FLOAT,"0");

//----	Local Parameters Declaration

//----	Settings Declaration
//[!output BB_FLAGS]
//[!output BB_CALLBACKREG]
	proto->SetFlags(CK_BEHAVIORPROTOTYPE_NORMAL);
	proto->SetFunction(BulletUnfreezeHinge);

	*pproto = proto;
	return CK_OK;
}

int BulletUnfreezeHinge(const CKBehaviorContext& BehContext)
{
	CKBehavior* beh = BehContext.Behavior;
	CKContext* cex = beh->GetCKContext();
	beh->ActivateInput(0, FALSE);
	beh->ActivateOutput(0, TRUE);

	btHingeConstraint* hingeConstraint = NULL;
	BulletManager* bManager =  (BulletManager*)cex->GetManagerByGuid(BulletManagerGUID);
	if(bManager)
	{
		int ID = -1;
		float lowerLimit, upperLimit, softness, biasFactor;
		beh->GetInputParameterValue(INPUT_PARAMENTER_CONSTRAINTID, &ID);
		beh->GetInputParameterValue(INPUT_PARAMENTER_LOWERLIMIT, &lowerLimit);
		beh->GetInputParameterValue(INPUT_PARAMENTER_UPPERLIMIT, &upperLimit);
		beh->GetInputParameterValue(INPUT_PARAMENTER_SOFTNESS, &softness);
		beh->GetInputParameterValue(INPUT_PARAMENTER_BIASFACTOR, &biasFactor);
		if(-1 != ID)
		{
			hingeConstraint = (btHingeConstraint*)bManager->getConstraint(ID);
			if(hingeConstraint)
			{
				hingeConstraint->setLimit(lowerLimit, upperLimit, softness, biasFactor);
			}
		}
	}
	return CKBR_OK;
}